package com.peanut.baby.mvp.trend;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.R;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.mvp.bbs.detail.BBSMomentDetailActivity;
import com.peanut.baby.mvp.profile.ProfileActivity;
import com.peanut.baby.mvp.trend.detail.TrendDetailActivity;
import com.peanut.baby.util.TimeUtil;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/28.
 */

public class TrendListAdapter extends HolderAdapter<BBSMoment, TrendListAdapter.Holder>{

    public TrendListAdapter(Context context, List<BBSMoment> listData) {
        super(context, listData);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, BBSMoment bbsMoment, int position) {
        return layoutInflater.inflate(R.layout.layout_item_trend,null);
    }

    @Override
    public Holder buildHolder(View convertView, BBSMoment bbsMoment, int position) {
        Holder holder = new Holder();
        holder.root = convertView;
        holder.avatar = (ImageView) convertView.findViewById(R.id.moment_avatar);
        holder.hasImage = (ImageView) convertView.findViewById(R.id.moment_image_flag);
        holder.nick = (TextView) convertView.findViewById(R.id.moment_nick);
        holder.time = (TextView) convertView.findViewById(R.id.moment_time);
        holder.category = (TextView) convertView.findViewById(R.id.moment_category);;
        holder.content = (TextView) convertView.findViewById(R.id.moment_content);;
        holder.scanCount = (TextView) convertView.findViewById(R.id.moment_scan_count);;
        holder.cmtCount = (TextView) convertView.findViewById(R.id.moment_comment_count);;
        return holder;
    }

    @Override
    public void bindViewDatas(Holder holder, final BBSMoment bbsMoment, int position) {
        ImageLoader.getInstance().loadImageView(context,bbsMoment.avatar,holder.avatar);
        holder.content.setText(Html.fromHtml(StringUtil.getSimpleText(bbsMoment.content)));
        holder.nick.setText(bbsMoment.nickname);
        holder.time.setText(TimeUtil.getDisplayTimeMillis(bbsMoment.updateTime));
        holder.hasImage.setVisibility(StringUtil.isNullOrEmpty(bbsMoment.imgUrl)? View.GONE : View.VISIBLE);
        holder.category.setText(bbsMoment.type);
        holder.scanCount.setText(bbsMoment.viewCount+"");
        holder.cmtCount.setText(bbsMoment.commentCount+"");
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrendDetailActivity.start(context,bbsMoment.id+"");
            }
        });

        holder.avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProfileActivity.start(context,bbsMoment.userId+"");
            }
        });



    }

    class Holder{
        View root;
        ImageView   avatar;
        ImageView   hasImage;
        TextView    nick;
        TextView    time;
        TextView    content;
        TextView    category;
        TextView    scanCount;
        TextView    cmtCount;

    }
}
